
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class BeerWindow {

	private static BeerWindow singleTonObj = null;
	
	private JFrame w = null;

	private BeerDraw beerMugs = null;
	private int mug1 = 0;
	private int mug2 = 0;
	
	private static Problem prob = null;
	private static int target = 0;

	private BeerWindow(String title) {
		this.w = new JFrame(title);
		w.setLayout(new GridBagLayout());
	}
	
	private void drawWindow() {
		
		GridBagConstraints c = new GridBagConstraints();
		
		c.ipadx = 50;
		c.ipady = 0;
		
		c.gridwidth = 2;
		c.gridx = 0;
		c.gridy = 0;
		
		beerMugs = new BeerDraw();
		w.add(beerMugs, c);
		
		c.gridwidth = 1;
		
		c.gridy++;
		
		w.add(new ButtonHelper("Umplere 1", new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int[] mug = prob.u1(mug1,mug2);
				mug1 = mug[0];
				mug2 = mug[1];
				
				checkState();
			}
		}),c);
		
		c.gridx++;
		
		w.add(new ButtonHelper("Umplere 2", new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int[] mug = prob.u2(mug1,mug2);
				mug1 = mug[0];
				mug2 = mug[1];
				
				checkState();
			}
		}),c);
		
		c.gridx = 0;
		c.gridy++;
		
		w.add(new ButtonHelper("Golire 1", new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int[] mug = prob.g1(mug1,mug2);
				mug1 = mug[0];
				mug2 = mug[1];
				
				checkState();
			}
		}),c);
		
		c.gridx++;
		
		w.add(new ButtonHelper("Golire 2", new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int[] mug = prob.g2(mug1,mug2);
				mug1 = mug[0];
				mug2 = mug[1];
				
				checkState();
			}
		}),c);
		
		c.gridx = 0;
		c.gridy++;
		
		w.add(new ButtonHelper("Transfer 1>2", new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int[] mug = prob.t12(mug1,mug2);
				mug1 = mug[0];
				mug2 = mug[1];
				
				checkState();
			}
		}),c);
		
		c.gridx++;
		
		w.add(new ButtonHelper("Transfer 2>1", new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int[] mug = prob.t21(mug1,mug2);
				mug1 = mug[0];
				mug2 = mug[1];
				
				checkState();
			}
		}),c);
		
		c.gridx = 0;
		c.gridy++;
		
		w.add(new ButtonHelper("DF", new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				prob.solve(2, 0);
				alert("AI Solving", "Check the console for logs");
			}
		}),c);
		
		c.gridx++;
		
		w.add(new ButtonHelper("BF", new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				prob.solve(2, 1);
				alert("AI Solving", "Check the console for logs");
			}
		}),c);
		
		w.setBounds(30,30,340,380);
//		w.pack();
	}
	
	public void showWindow() {
		drawWindow();
		
		w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		w.setVisible(true);
		w.setLocationRelativeTo(null);
	}
	
	public void checkState() {
		if (mug1 == target || mug2 == target) {
			alert("Awesome!", "You have solved the problem.");
		}
		
		System.out.println("M1="+mug1+", M2="+mug2);
		beerMugs.updateDraw();
	}
	
	public Problem getProb() {
		return prob;
	}
	
	public int getMug1() {
		return mug1;
	}
	
	public int getMug2() {
		return mug2;
	}
	
	public void resetApp() {
		mug1 = 0;
		mug2 = 0;
		beerMugs.updateDraw();
	}
	
	/* ------------------------------------------------------- */

	public static BeerWindow init(Problem p, int t) {
		if (singleTonObj == null) {
			singleTonObj = new BeerWindow("Beers");
			
			prob = p;
			target = t; 
		}

		return singleTonObj;
	}
	
	public static BeerWindow get() {
		return singleTonObj;
	}
	
	public static void reset() {
		get().resetApp();
	}
	
	public static void alert(String title, String text) {
		final JFrame frame = new JFrame(title);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.setLayout(new GridLayout(2,1));
		
		JLabel lbl = new JLabel(text);
		lbl.setHorizontalAlignment(JLabel.CENTER);
		frame.add(lbl);
		frame.add(new ButtonHelper("OK!", new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset();
				frame.dispose();
			}
		}));
		
		frame.setVisible(true);
		frame.setBounds(200, 200, 200, 100);
		frame.setLocationRelativeTo(null);
	}
}